$(function() {
	create_account.initial();
});

var create_account = {

	initial : function initial() {
		this.bindSubmitForm();
	},

	bindSubmitForm : function bindSubmitForm() {
		$('#btn-reg').stop().click(function () {
            var result = create_account.verifyInput();
            if (!result) return;
            var data = new Object();
            data.username = $("#name").val();
            data.truename = $("#truename").val();
            data.password = $("#set_password").val();
            data.company = $("#select-school-input").val();
            data.department = $("#select-grade-input").val();
            data.studClass = $("#select-class-input").val();
            data.studSex = $("#select-sex-input").val();
            jQuery
                .ajax({
                    headers : {
                        'Accept' : 'application/json',
                        'Content-Type' : 'application/json'
                    },
                    type : "POST",
                    url : "/user-reg",
                    data : JSON.stringify(data),
                    success : function(message, tst, jqXHR) {
                        if (message.result == "success") {
                            document.location.href = document
                                    .getElementsByTagName('base')[0].href
                                + "regist-success/"
                                + data.username;
                        } else {
                            if (message.result == "duplicate-username") {
                                $(
                                    ".form-username .form-message")
                                    .text(
                                        message.messageInfo);
                            } else if (message.result == "captch-error") {

                            } else if (message.result == "duplicate-email") {
                                $(
                                    ".form-email .form-message")
                                    .text(
                                        message.messageInfo);
                            } else {
                                alert(message.result);
                            }
                        }
                    }
                });
        });
	},

	verifyInput : function verifyInput() {
		$(".form-message").empty();
		var check_u = this.checkUsername();
		if(!check_u) return check_u;

		var check_p = this.checkPassword();
        if(!check_p) return check_p;

		var check_cp = this.checkConfirmPassword();
        if(!check_cp) return check_cp;

		var check_job = this.checkJob();
        if(!check_job) return check_job;

        var check_up = this.checkTruename();
        if(!check_up) return check_up;

        var check_sex = this.checkUserSex();
        if(!check_sex) return check_sex;

		return true;
	},
    checkUserSex: function checkUserSex() {
        if($("#select-sex-input").val() == 0){
            $(".form-sex-select .form-message").text("请选择性别");
            return false;
        }
        return true;
    },
	checkTruename: function checkTruename() {
        var turename = $(".form-turename input").val();
        if (turename == "") {
            $(".form-turename .form-message").text("姓名不能为空");
            return false;
        }
        return true;
    },
	checkUsername : function checkUsername() {
		var username = $(".form-username input").val();
		if (username == "") {
			$(".form-username .form-message").text("用户名不能为空");
			return false;
		} else if (username.length > 20 || username.length < 5) {
			$(".form-username .form-message").text("请保持在5-20个字符以内");
			return false;
		} else {
			var re=/[\+|\-|\\|\/||&|!|~|@|#|\$|%|\^|\*|\(|\)|=|\?|´|"|<|>|\.|,|:|;|\]|\[|\{|\}|\|]+/;
			if(re.test(username)){
				$(".form-username .form-message").text("只能是数字字母或者下划线的组合");
				return false;
			}
		}

		return true;
	},

	checkPassword : function checkPassword() {
		var password = $(".form-password input").val();
		if (password == "") {
			$(".form-password .form-message").text("密码不能为空");
			return false;
		} else if (password.length < 6 || password.length > 20) {
			$(".form-password .form-message").text("密码请保持在6到20个字符以内");
			return false;
		} else {
			return true;
		}
		return true;
	},

	checkConfirmPassword : function checkConfirmPassword() {
		var password_confirm = $(".form-password-confirm input").val();
		var password = $(".form-password input").val();
		if (password_confirm == "") {
			$(".form-password-confirm .form-message").text("请再输入一次密码");
			return false;
		} else if (password_confirm.length > 20) {
			$(".form-password-confirm .form-message").text(
					"内容过长，请保持在20个字符以内");
			return false;
		} else if (password_confirm != password) {
			$(".form-password-confirm .form-message").text("2次密码输入不一致");
			return false;
		} else {
			return true;
		}
	},
	checkJob : function(){
		if($("#select-school-input").val() == -1){
			$(".form-school-select .form-message").text("请选择学校");
			return false;
		}
        if($("#select-grade-input").val() == -1){
            $(".form-grade-select .form-message").text("请选择年级");
            return false;
        }
        if($("#select-class-input").val() == -1){
            $(".form-class-select .form-message").text("请选择班级");
            return false;
        }
		return true;
	}

};